# -*- coding: utf-8 -*- 

###################### Replacing this file with main.py uses the API ######################

from google.appengine.api import users
import webapp2
import jinja2
import random
import string
import os
import dal
import httpRequests
import hashlib
import time
import logging
from google.appengine.api import urlfetch
from CQutils import  EQueueStatus

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

def get_qr_link(request):
    port = request.environ["SERVER_PORT"]
    proto = request.environ["SERVER_PROTOCOL"].split("/")[0]
    if 'Development' in os.environ['SERVER_SOFTWARE']:
        result = urlfetch.fetch(r'http://curlmyip.com/')
        if result.status_code == 200:
            hostname = result.content
            # hostname = "10.0.0.2"
    else:
        hostname = 'Development' in os.environ['SERVER_SOFTWARE']
    return "http://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=%s://%s:%s/user_page" % (proto, hostname, port)

def generate_random_id():
    return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(5))


      
###################### HTML Pages Classes ######################

class MainPage(webapp2.RequestHandler):
    def get(self):
        admin = users.get_current_user()
        logout_url = users.create_logout_url(self.request.uri)
        login_url = users.create_login_url(self.request.uri)

        if admin:
            admin_state = "Yes"
            admin_name = admin.nickname()
        else:
            admin_state = "No"
            admin_name = ''

        template_values = {
            'admin_name': admin_name,
            'login_url': login_url,
            'logout_url': logout_url,
            'admin_state': admin_state,
        }
        template = jinja_environment.get_template('webpages/index3.html')
        self.response.out.write(template.render(template_values))
   

class AdminPage(webapp2.RequestHandler):
    def get(self):
        update_interval = 1000
        admin = users.get_current_user()
        if (admin == None):
            login_url = users.create_login_url(self.request.uri)
            self.redirect(login_url)

        else:  
            admin_name = admin.nickname()
            admin_id = admin.user_id()
            queues = dal.get_all_queues_from_admin_by_id(admin_id)
            if len(queues) == 0:
                queue_id = "None"
                queue_name = None
                queue_state = None
                queue_op = None
                people_inline = None
                logout_url = None
                current_user = None
                current_phone = None
                statistics = "0 0 0 0"
                queue_type = "0"
            else:
                queue_id = queues[0]
                queue_type = httpRequests.get_queue_type(queue_id)
                queue_name = httpRequests.get_queue_name(queue_id)
                queue_name = queue_name.replace(" ", "-")
                status = httpRequests.get_queue_status(queue_id)
                if status == EQueueStatus.TERMINATED:            
                    queue_state = 'Non-active'
                    queue_op = 'Activate'            
                else:
                    queue_state = 'Active'
                    queue_op = 'Terminate'
                temp = dal.get_queue_statistics(queue_id)
                statistics = str(int(temp[0])) + " " + str(int(temp[1])) + " " + str(int(temp[2])) + " " + str(int(temp[3]))
                people_inline = str(httpRequests.get_queue_size(queue_id))
                logout_url = users.create_logout_url('/');  # need to be changed to appspot.com url
                inservice_users = httpRequests.get_inservice_users(queue_id)
                logging.debug("value of inservice_users = %s", inservice_users)
                if len(inservice_users) > 0:
                    current_user = inservice_users[0][0]
                    user_details = httpRequests.get_user_details(current_user)
                    current_user = user_details["name"]
                    current_phone = user_details["phone"]
                    current_user = current_user.replace("-", " ")
                    if current_phone == None:
                        current_phone = ""
                else:
                    (current_user, current_phone) = ("None", " 0 ")
            template_values = {
                'admin_name':admin_name,
                'queue_name':queue_name,
                'queue_state':queue_state,
                'queue_op':queue_op,
                'people_inline':people_inline,
                'queue_id':queue_id,
                'logout_url':logout_url,
                'current_user':current_user,
                'current_phone':current_phone,
                'statistics':statistics,
                'update_interval': update_interval,
                'queue_type' : queue_type,
            }

            template = jinja_environment.get_template('webpages/admin_page4.html')
            self.response.out.write(template.render(template_values))


class UserPage(webapp2.RequestHandler): 
    def get(self): 
        queue_type = self.request.get('type')
        queue_id = str(self.request.get("queue_id"))
        queue_name = httpRequests.get_queue_name(queue_id)
        user_id = self.request.cookies.get("CQ_userid")
        if user_id == "":
            user_id = None
        user_name = user_phone = ""
               
        if dal.is_valid_queue(queue_id) == False:
            self.response.out.write("error: queue doesn't exist")
            return

        if user_id is None or dal.is_valid_user(user_id) == False:  # user doesn't exist so redirect it to create user page
            template_values = {
                'queue_id': queue_id,
                'queue_name': queue_name,
                'queue_type': queue_type,
                'user_name': user_name,
                'user_phone': user_phone,
            }
            template = jinja_environment.get_template('webpages/add_user.html')
            self.response.out.write(template.render(template_values))
            return
          
          
        user_pos_id = dal.get_user_pos_in_queue(user_id, queue_id)
        user_in_this_queue = user_pos_id is not None
        user_wainting_anoter_pos = False
        for p in dal.get_user_positions(user_id):
            if p != user_pos_id and httpRequests.get_pos_number(p) >= 0:
                user_wainting_anoter_pos = True
          
        if user_wainting_anoter_pos is True:
            self.response.out.write("you are already waiting in another queue") 
            return
        
        if user_in_this_queue is False:
            httpRequests.join_queue(user_id, queue_id)
          
        user_details = httpRequests.get_user_details(user_id)
        if user_details is not None:
            user_name = user_details.get("name", "")
            user_phone = user_details.get("phone", "")
                   
        pos_id = dal.get_user_pos_in_queue(user_id, queue_id)
        user_status = httpRequests.get_position_status(pos_id)
    
        update_interval = "5000"
        if (user_status == 0):
            update_interval = "2000"
   
        template_values = {
            'queue_name': queue_name,
            'queue_id': queue_id,
            'user_id': user_id,
            'user_name': user_name,
            'user_phone': user_phone,
            'user_status': user_status,
            'update_interval': update_interval,
            }
               
        template = jinja_environment.get_template('webpages/user_page.html')
        self.response.out.write(template.render(template_values))
     
        
########## 
class ManualPage(webapp2.RequestHandler):
    def get(self):
        queue_id = self.request.get('queue_id')
        queue_type = self.request.get('type')
        queue_name = httpRequests.get_queue_name(str(queue_id))
        position = -1
        template_values = {
            'qr_url': get_qr_link(self.request),
            'queue_name':queue_name,
            'queue_id':queue_id,
            'position':position,
            'queue_type':queue_type,
            }    
        template = jinja_environment.get_template('webpages/manual_page.html')
        self.response.out.write(template.render(template_values))


########### 
class PrintQr(webapp2.RequestHandler):
    def get(self):
        queue_id = self.request.get('queue_id')     
        queue_type = self.request.get('type')
        queue_name = httpRequests.get_queue_name(str(queue_id))
        template_values = {
            'qr_url':get_qr_link(self.request),
            'queue_id':queue_id,
            'queue_type':queue_type,
            'queue_name':queue_name
            }
        template = jinja_environment.get_template('webpages/print_qr.html')
        self.response.out.write(template.render(template_values))


class HelpPage(webapp2.RequestHandler):
    def get(self):
        admin_name = users.get_current_user()
        if (admin_name):
            login_url = users.create_logout_url(self.request.uri)
            login_text = u"התנתק"
        else:
            login_url = users.create_login_url(self.request.uri)
            login_text = u"התחבר"
            admin_name = u"אינך מחובר"
        template_values = {
        'admin_name':admin_name,
        'login_url':login_url,
        'login_text':login_text
        }
        template = jinja_environment.get_template('webpages/help.html')
        self.response.out.write(template.render(template_values))

class UserPagePreview(webapp2.RequestHandler):
    def get(self):

        template_values = {}  
          
        template = jinja_environment.get_template('webpages/user_page_preview.html')
        self.response.out.write(template.render(template_values))
        

###################### Get Data Classes ######################

########## USER DATA #############        
class GetPosition(webapp2.RequestHandler):
    def get(self):
        pos_id = self.request.get('pos_id')
        queue_id = self.request.get('queue_id')
        user_id = self.request.get('user_id')
        if user_id is None or user_id == "":
            user_id = self.request.cookies.get("CQ_userid")
        position = -2
        if user_id != "" and  queue_id != "":
            pos_id = dal.get_user_pos_in_queue(user_id, queue_id)
        if pos_id is not None and pos_id != "":
            position = httpRequests.get_pos_number(pos_id)
        self.response.out.write(str(position))
         

class GetTimeEstimate(webapp2.RequestHandler):
    def get(self):
        queue_id = self.request.get('queue_id')
        logging.debug("queue_id = %s", queue_id)
        user_id = self.request.get('user_id')
        logging.debug("user_id = %s", user_id)
        queue_stats = dal.get_queue_statistics(queue_id)
        average_time = queue_stats[1]
        user_position = httpRequests.get_pos_number(dal.get_user_pos_in_queue(user_id, queue_id))
        elapsed_time = dal.get_last_service_delta(queue_id)
        estimated_time = (average_time * user_position - elapsed_time) / 60
        if user_position > 0:
            if estimated_time > 0:    
                self.response.out.write(int(estimated_time))
            else:
                self.response.out.write(0)
                
                
######   ADMIN DATA #########
class GetSize(webapp2.RequestHandler):
    def get(self):
        queue_id = self.request.get('queue_id')
        people_inline = str(httpRequests.get_queue_size(queue_id))
        self.response.out.write(people_inline)


class GetQueues(webapp2.RequestHandler):
    def get(self):
        admin = users.get_current_user()
        admin_id = admin.user_id()
        queues = dal.get_all_queues_from_admin_by_id(admin_id)
        data = ""
        data_m = ""
        for queue in queues:
            name = httpRequests.get_queue_name(queue)
            data = data + u'<li><div class="row-fluid nav-tabs"><a class="btn span3" data-toggle="tooltip" title="מחק תור" href="javascript:delete_queue(\'' + str(queue) + '\')"><img src="/img/1378081752_trash.png" width="25" height="25"></img></a><a class="btn span9" style="text-align:right;" onclick="choose_queue(\'' + str(queue) + '\')">' + name + '</a></div></li>'
            data_m = data_m + u'<li><a style="text-align:right;" href="javascript:choose_queue(\'' + str(queue) + '\');">' + name + '</a></li>'
            
        self.response.out.write(data + "*****" + data_m)      
        
 
class GetQueueData(webapp2.RequestHandler):
    def get(self):
        queue_id = self.request.get('name')
        name = httpRequests.get_queue_name(queue_id)
        name = name.replace(" ", "-")
        state = dal.get_queue_status(queue_id)
        status = "Active"
        if state == 1:
            status = "Non-active"
        people_inline = str(httpRequests.get_queue_size(queue_id))
        queue_type = httpRequests.get_queue_type(queue_id)
        inservice_users = httpRequests.get_inservice_users(queue_id)
        logging.debug("value of inservice_users = %s", inservice_users)  
        if len(inservice_users) > 0:
            current_user = inservice_users[0][0]
            user_details = httpRequests.get_user_details(current_user)
            current_phone = user_details["phone"]
            if queue_type == 2:
                current_name = current_user
            else: 
                current_name = user_details["name"]
                current_name = current_name.encode('UTF-8')
                
        else:
            current_name = "None"
            current_phone = "x"
        data = people_inline + " " + status + " " + name.encode('UTF-8') + " " + str(queue_id) + " " + str(current_name) + " " + str(current_phone) + " " + str(queue_type)
        self.response.out.write(data)      
 
class GetQueueUsers(webapp2.RequestHandler):
    def get(self):
        
        # increasing the default urlfetch deadline value
        urlfetch.set_default_fetch_deadline(45)
        
        queue_id = self.request.get('queue_id')
        queue_users = httpRequests.get_next_users(queue_id, 10)
        logging.debug("value of queue_users = %s", queue_users)
        queue_size = httpRequests.get_queue_size(queue_id)
        pos = 1
        table_header = u"""
        <table class="table table-bordered table-hover">
          <caption>ישנם """ + str(queue_size) + u""" אנשים בתור </caption>
            <thead>
              <tr>
                <th style="text-align:right">מיקום</th>
                <th style="text-align:right">שם</th>
                <th style="text-align:right">טלפון</th>
              </tr>
            </thead>
            <tbody>
        """
        table_tail = u"</tbody></table>"
        table_data = ""
        for user in queue_users:
            user_details = httpRequests.get_user_details(user)
            user_name = user_details["name"]
            user_phone = user_details["phone"]
            table_data = table_data + u"<tr><th style='text-align:right'>" + str(pos) + "</th><td style='text-align:right'>" + user_name + "</td><td style='text-align:right'>" + user_phone + "</td></tr>\n"
            pos = pos + 1
        output = table_header + table_data + table_tail    
        self.response.out.write(output) 
        

class GetQueueStatistics(webapp2.RequestHandler):
    def get(self):
        queue_id = self.request.get('queue_id')
        temp = dal.get_queue_statistics(queue_id)
        statistics = str(int(temp[0])) + " " + str(int(temp[1])) + " " + str(int(temp[2])) + " " + str(int(temp[3]))
        self.response.out.write(statistics)


class QueueOperation(webapp2.RequestHandler):
    def get(self):
        working_queue = self.request.get('queue_id')
        status = dal.get_queue_status(working_queue)
        if (status == 0):
            httpRequests.stop_queue(working_queue)
            self.response.out.write("Non-active")
        else:
            httpRequests.resume_queue(working_queue)
            self.response.out.write("Active")
        # self.redirect('/admin_page')


class GetExtension(webapp2.RequestHandler):
    def get(self):
        queue_id = self.request.get('queue_id')
        user_id = self.request.get('user_id')  
        user_ext = [ext for (u_cqid, ext) in httpRequests.get_inservice_users(queue_id) if u_cqid == user_id]
        extention = -1
        if len(user_ext) > 0:
            extention = user_ext[0]
        self.response.out.write(str(extention))


class GetImage(webapp2.RequestHandler):
    def get(self):
        queue_id = str(self.request.get('queue_id'))
        image = httpRequests.get_queue_image(queue_id)
        if (image):
            self.response.out.write(image)
        else:
            self.redirect("/img/template_background.jpg")


###################### Post Data Classes ######################

class AddQueue(webapp2.RequestHandler):
    def post(self):
        creation_time = str(round(time.time()))
        admin = users.get_current_user()
        admin_id = admin.user_id()
        queue_name = self.request.get('queue_name')
        queue_time = self.request.get('queue_time')
        queue_type = self.request.get('queue_type')
        queue_img = str(self.request.get('queue_img'))
        queue_hash = hashlib.md5(creation_time + admin_id + queue_name.encode("utf-16"))
        queue_id = queue_hash.hexdigest()
        if queue_time is None or queue_time == "":
            queue_time = 0 
            
        result = httpRequests.create_queue_by_id(admin_id, queue_name, queue_id, int(queue_type), int(queue_time), queue_img, )

        if result is 0:
            time.sleep(3)
            return self.redirect("/admin_page")
        if result is -1:
            template = jinja_environment.get_template('webpages/queue_exist.html')
            self.response.out.write(template.render({'queue_name': queue_name, }))
                
    
class DeleteQueue(webapp2.RequestHandler):
    def get(self):
        queue_id = self.request.get('queue_id') 
        httpRequests.delete_queue(str(queue_id))
        # self.response.out.write(httpRequests.get_queue_name(queue_id) + "- has been deleted!" + str(dal.get_queue_status(queue_id)))    
        self.response.out.write(httpRequests.get_queue_name(queue_id))    
     
     
########## USER OPERATIONS #############
class ManualJoin(webapp2.RequestHandler):
    def get(self):
        queue_id = str(self.request.get('queue_id'))
        user_name = self.request.get('user_name').replace(" ", "-")
        user_phone = self.request.get('user_phone').replace(" ", "").replace("-", "")
        queue_state = str(dal.get_queue_status(queue_id))
        if (user_phone == ""):
            user_phone = "x"    
        if (queue_state == '0'):
            user_details = {"name":user_name, "phone":user_phone}
            user_id = httpRequests.create_user(user_details)
            httpRequests.join_queue(user_id, queue_id)
            self.response.out.write(str(user_id))
        else:
            self.response.out.write("None")
     
           
class UserCreate(webapp2.RequestHandler):
    def get(self):        
        queue_id = self.request.get('queue_id')  
        user_name = self.request.get('user_name').replace(" ", "-")
        user_phone = self.request.get('user_phone')
        if (user_phone == ""):
            user_phone = "x"
        user_id = httpRequests.create_user({"name":user_name, "phone":user_phone})       
        self.response.set_cookie("CQ_userid", user_id)  
        self.redirect("/user_page?queue_id=" + queue_id)
        
       
class UserRejoin(webapp2.RequestHandler):
    def get(self):
        queue_id = self.request.get('queue_id')
        user_id = self.request.cookies.get("CQ_userid")
        httpRequests.join_queue(user_id, queue_id)
        self.redirect("/user_page?queue_id=" + queue_id)
                    

class DelUser(webapp2.RequestHandler):
    def get(self):
        
        # increasing the default urlfetch deadline value
        urlfetch.set_default_fetch_deadline(45)
        
        queue_id = self.request.get('queue_id')
        ext = self.request.get('ext')
        httpRequests.advance_queue(queue_id, ext)
        queue_type = httpRequests.get_queue_type(queue_id)
        inservice_users = httpRequests.get_inservice_users(queue_id)
        logging.debug("value of inservice_users = %s", inservice_users)
        if (len(inservice_users) > 0):
            current_user = inservice_users[0][0]
            user_details = httpRequests.get_user_details(current_user)
            current_phone = user_details["phone"]
            if queue_type == 2:
                current_name = current_user
            else: 
                current_name = user_details["name"]
                current_name = current_name.encode('UTF-8')
        else:
            current_name = "None"
            current_phone = "x"
        self.response.out.write(str(httpRequests.get_queue_size(queue_id)) + " " + str(current_name) + " " + current_phone.encode('UTF-8'))


class UserLeave(webapp2.RequestHandler):
    def get(self):
        user_id = self.request.cookies.get("CQ_userid")
        queue_id = self.request.get('queue_id')
        httpRequests.leave_queue(dal.get_user_pos_in_queue(user_id, queue_id))
        self.redirect("/user_page?queue_id=" + queue_id)


class CallNext(webapp2.RequestHandler):
    def get(self):
        admin = users.get_current_user()
        admin_id = admin.user_id()
        queues = dal.get_all_queues_from_admin_by_id(admin_id)
        queue_id = queues[0]
        next_user = httpRequests.advance_queue(queue_id)
        self.response.out.write(str(next_user))


class SetImage(webapp2.RequestHandler):
    def post(self):
        queue_id = str(self.request.get('queue_id'))
        image = str(self.request.get('imgInp'))
        if (image):
            httpRequests.set_queue_image(queue_id, image)
        return self.redirect("/admin_page")



class DelImage(webapp2.RequestHandler):
    def get(self):
        queue_id = str(self.request.get('queue_id'))
        httpRequests.del_queue_image(queue_id)
  


class PosQueue(webapp2.RedirectHandler):
    def get(self):
        pos_cqid = str(self.request.get('pos_id'))
        self.response.out.write(dal.get_queue_by_position(pos_cqid))
        

###################### Misc Classes ######################

class LogoutCheck(webapp2.RequestHandler):
    def get(self):
        queue_id = str(self.request.get('queue_id'))
        b = (httpRequests.get_queue_size(queue_id) == 0) and (dal.get_queue_status(queue_id) == EQueueStatus.TERMINATED)
        self.response.out.write(b)  


class Test(webapp2.RequestHandler): 
    def get(self):
        queue_id = str(self.request.get('queue_id'))
        if (queue_id):
            queue_image = str(httpRequests.get_queue_image(queue_id))  
            self.response.headers['Content-Type'] = 'image/png'
            self.response.out.write(queue_image) 
        else:
            template = jinja_environment.get_template('webpages/test.html')
            self.response.out.write(template.render())
             
    def post(self):
        image = str(self.request.get('img'))
        queue_id = str(self.request.get('queue_id'))
        httpRequests.set_queue_image(queue_id, image)
        
        self.response.headers['Content-Type'] = 'image/png'
        self.response.out.write(image)
      
 
class ClearDB(webapp2.RequestHandler):  
    def get(self):
        from google.appengine.ext import db
        from google.appengine.api import memcache
        import models
  
        db.delete(models.DSQueue.all(keys_only=True))
        db.delete(models.DSPosition.all(keys_only=True))
        db.delete(models.DSAdmin.all(keys_only=True))
        db.delete(models.DSUser.all(keys_only=True))
        memcache.Client().flush_all()
        self.response.set_cookie("CQ_userid", None) 
        self.response.out.write("all cleared")


###################### APPLICATION #######################
class GetAllPosition(webapp2.RequestHandler):
    def get(self):
        user_cqid = self.request.get('user_id')
        out = "" 
        if user_cqid != "":
            positions = dal.get_user_positions(user_cqid)
            for pos_id in positions:
                out += pos_id + "\n"
        self.response.out.write(out)

        
###################### Classes Source  ######################

app = webapp2.WSGIApplication([('/test', Test),
                                ('/get_position', GetPosition),
                                ('/get_time_estimate', GetTimeEstimate),
                                ('/get_size', GetSize),
                                ('/get_image', GetImage),
                                ('/set_image', SetImage),
                                ('/del_image', DelImage),
                                ('/del_user', DelUser),
                                ('/add_queue', AddQueue),
                                ('/user_rejoin', UserRejoin),
                                ('/user_leave', UserLeave),
                                ('/user_page', UserPage),
                                ('/admin_page', AdminPage),
                                ('/print_qr', PrintQr),
                                ('/operate_queue', QueueOperation),
                                ('/call_next', CallNext),
                                ('/logout_check', LogoutCheck),
                                ('/manual_page', ManualPage),
                                ('/manual_join', ManualJoin),
                                ('/auto_join', UserCreate),
                                ('/get_queues', GetQueues),
                                ('/delete_queue', DeleteQueue),
                                ('/get_queue_data', GetQueueData),
                                ('/get_queue_users', GetQueueUsers),
                                ('/get_queue_statistics', GetQueueStatistics),
                                ('/clearDB', ClearDB),
                                ('/get_extension', GetExtension),
                                ('/pos_queue', PosQueue),
                                ('/get_all_positions', GetAllPosition),
                                ('/help', HelpPage),
                                ('/user_page_preview', UserPagePreview),
                                ('/', MainPage)], debug=True)